#ifndef ARBITRARYWAIT_H_
#define ARBITRARYWAIT_H_

#include "pthread.h"
#include "WaitObject.h"

class ArbitraryWait {
public:
	ArbitraryWait();
	virtual ~ArbitraryWait();

	unsigned int addWaitObject(int sockfd, char* buffer, int length);
	unsigned int addWaitObject(pthread_mutex_t*, pthread_cond_t*);
	unsigned int addWaitObject(WaitObject* newWaitObject);
	unsigned int addPthreadWaitObject();
	void deleteWaitObject(unsigned int index);

	unsigned int wait();
	void abandonWait();
	void signalWaitingObject();

	void getLock();
	void giveLock();

	unsigned int errsig;

private:
	pthread_mutex_t listlock;
	pthread_mutex_t waitlock;
	pthread_cond_t waitcond;
	pthread_rwlock_t transferlock;
	pthread_mutex_t gglock;
	pthread_cond_t ggcond;
	WaitObject** objs;
	int numobjs;

	static void* waiter(void* waitObjectVoid);
};

#endif /* ARBITRARYWAIT_H_ */
